package com.tanhua.commons.template;

import com.baidu.aip.contentcensor.AipContentCensor;
import com.baidu.aip.contentcensor.EImgType;
import com.tanhua.commons.properties.ContentCensorProperties;
import org.json.JSONObject;

import java.util.List;

/**
 * @author liuyp
 * @date 2021/02/24
 */
public class ContentCensorTemplate {

    private AipContentCensor client;

    public ContentCensorTemplate(ContentCensorProperties properties) {
        this.client = new AipContentCensor(properties.getAppId(), properties.getApiKey(), properties.getSecretKey());
    }

    /**
     * 文本内容审核
     *
     * @param text 要审核的文本内容
     * @return 审核是否通过
     */
    public boolean textCensor(String text) {
        //2. 调用接口进行文本内容审核
        JSONObject response = client.textCensorUserDefined(text);
        //3. 返回结果   conclusionType 1.合规，2.不合规，3.疑似，4.审核失败
        return 1 == response.getInt("conclusionType");
    }


    /**
     * 图像内容审核
     *
     * @param imageUrl 图片链接地址
     * @return 审核是否通过
     */
    public boolean imageCensor(String imageUrl) {
        JSONObject jsonObject = client.imageCensorUserDefined(imageUrl, EImgType.URL, null);
        return 1 == jsonObject.getInt("conclusionType");
    }

    /**
     * 图像的批量审核
     * @param urlList
     * @return
     */
    public boolean imageBatchCensor(List<String> urlList){
        for (String url : urlList) {
            boolean result = imageCensor(url);
            if (!result) {
                return false;
            }
        }

        return true;
    }
}
